package com.lb.jwitter.action;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;

import com.lb.jwitter.service.ConditionReport;
import com.lb.jwitter.service.TblQLDTTXABienNhanHoSo;
import com.lb.jwitter.service.TblQLDTTXABienNhanHoSoGiayTo;
import com.lb.jwitter.service.TblQLDTTXABienNhanHoSoGiayToService;
import com.lb.jwitter.service.TblQLDTTXABienNhanHoSoService;
import com.lb.jwitter.service.TblQuyenChiTietService;
import com.lb.jwitter.service.TblTaiKhoan;
import com.lb.jwitter.util.ApplicationContextProvider;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.tutego.jrtf.Rtf;
import com.tutego.jrtf.RtfTemplate;


@ContextConfiguration(locations="classpath:beans.xml") 
public class TblQLDTTXABienNhanHoSoExportAction {
	  
	  private String filename;
	  private String gioiTinh;
	  private Long bienNhanHoSoId;
	  
	  
	  private TblTaiKhoan taiKhoanDangNhap; 
	  public TblTaiKhoan getTaiKhoanDangNhap() { 
	      return taiKhoanDangNhap; 
	  } 
	  public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
	      this.taiKhoanDangNhap = taiKhoanDangNhap; 
	  } 
	  private TblQuyenChiTietService tblQuyenChiTietService;
	 	 @Autowired // Injected by Spring 
	      ApplicationContextProvider contextProvider; 
	 	 
	  public String execute() throws Exception {
			try{
				Map session = ActionContext.getContext().getSession();
			     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
			     if( taiKhoanDangNhap == null) return "noprivilege";
			     
			    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
			    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
			    
//			    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_BienNhanHoSo", "sua")){ 
//			        return "noprivilege"; 
//			    }        
			    
			    TblQLDTTXABienNhanHoSoService tblQLDTTXABienNhanHoSoService = (TblQLDTTXABienNhanHoSoService) context.getBean("tbl_QLDTTXA_BienNhanHoSoClient"); 
			    TblQLDTTXABienNhanHoSoGiayToService tblQLDTTXABienNhanHoSoGiayToService = (TblQLDTTXABienNhanHoSoGiayToService) context.getBean("tbl_QLDTTXA_BienNhanHoSo_GiayToClient"); 
			       
			    if(bienNhanHoSoId != null && !bienNhanHoSoId.equals(new Long(0))){
				    TblQLDTTXABienNhanHoSo tblQLDTTXABienNhanHoSo = tblQLDTTXABienNhanHoSoService.gettblQLDTTXABienNhanHoSo(bienNhanHoSoId);
				    if(tblQLDTTXABienNhanHoSo != null){
				    	
				    	String maFile = tblQLDTTXABienNhanHoSo.getHoSoId().getMaHoSo().replaceAll("[-+/*&%$#@!.^:,]","");
				        
				    	filename ="OutFile/" + "BienNhanHoSo" + maFile + ".rtf";
				    	//filename ="OutFile/" + "BienNhanHoSo" + tblQLDTTXABienNhanHoSo.getHoSoId().getMaHoSo()+ ".rtf";
				    	
						RtfTemplate rtfTemplate = Rtf.template( new FileInputStream("file/TemplateFile/bienNhanHoSoTemplate.rtf") );
						
						rtfTemplate.inject( "maHoSo", tblQLDTTXABienNhanHoSo.getHoSoId().getMaHoSo() );
						rtfTemplate.inject( "hoTen", tblQLDTTXABienNhanHoSo.getHoSoId().getHoTen().toUpperCase() + " " + tblQLDTTXABienNhanHoSo.getHoSoId().getTen().toUpperCase());
						if(tblQLDTTXABienNhanHoSo.getHoSoId().isGioiTinh())
							rtfTemplate.inject( "gioiTinh", "Nam");
						else
							rtfTemplate.inject("gioiTinh", "Nu");
						
	//					rtfTemplate.inject("gioiTinh", gioiTinh);

						rtfTemplate.inject( "ngaySinh", 
									tblQLDTTXABienNhanHoSo.getHoSoId().getNgaySinh().getDay()+ "/" 
									+ tblQLDTTXABienNhanHoSo.getHoSoId().getNgaySinh().getMonth()+ "/" 
									+ tblQLDTTXABienNhanHoSo.getHoSoId().getNgaySinh().getYear());
							
						
						rtfTemplate.inject( "noiSinh", tblQLDTTXABienNhanHoSo.getHoSoId().getNoiSinh() );
						rtfTemplate.inject( "soBienLai", tblQLDTTXABienNhanHoSo.getSoBienLai() );
						rtfTemplate.inject( "ngayDangKy", "");
	
						
						rtfTemplate.inject( "ghiChu", tblQLDTTXABienNhanHoSo.getGhiChu() );
						
						//print danh muc giay to
						List<ConditionReport> cons = new ArrayList<ConditionReport>();
						if (bienNhanHoSoId != null && !bienNhanHoSoId.equals(new Long(0))) 
						{ 
						       ConditionReport con = new ConditionReport(); 
						       con.setCol("bienNhanId.id"); 
						       con.setValue1(new Long(bienNhanHoSoId)); 
						       con.setOperator("eq"); 
						       cons.add(con); 
						} 
						List<TblQLDTTXABienNhanHoSoGiayTo> tblQLDTTXABienNhanHoSoGiayTos = tblQLDTTXABienNhanHoSoGiayToService.findtblQLDTTXABienNhanHoSoGiayToByCondition(cons, 0, 0); 
						  
						String danhMucGiayTo = "";
						for(TblQLDTTXABienNhanHoSoGiayTo tblQLDTTXABienNhanHoSoGiayTo : tblQLDTTXABienNhanHoSoGiayTos){
							danhMucGiayTo += "" + tblQLDTTXABienNhanHoSoGiayTo.getGiayToId().getTen() + " \n";
						}
						
						rtfTemplate.inject( "danhMucGiayTo", danhMucGiayTo );
						
						rtfTemplate.inject( "ngay", tblQLDTTXABienNhanHoSo.getNgayNhan().getDay() );
						rtfTemplate.inject( "thang", tblQLDTTXABienNhanHoSo.getNgayNhan().getMonth());
						rtfTemplate.inject( "nam", tblQLDTTXABienNhanHoSo.getNgayNhan().getYear() );
						rtfTemplate.inject( "nguoiNhan", tblQLDTTXABienNhanHoSo.getCanBoId().getTen().toUpperCase() );
						
						
						
						rtfTemplate.out( new FileOutputStream("file/" +filename) );
				    }
			    }

			
			return "download"; 
			}catch(Exception e){
				 e.printStackTrace(); 
				 return Action.ERROR; 
			}
		}

	public String getFilename() {
		return filename;
	}

	public void setFilename(String filename) {
		this.filename = filename;
	}
	public Long getBienNhanHoSoId() {
		return bienNhanHoSoId;
	}
	public void setBienNhanHoSoId(Long bienNhanHoSoId) {
		this.bienNhanHoSoId = bienNhanHoSoId;
	}
	public String getGioiTinh() {
		return gioiTinh;
	}
	public void setGioiTinh(String gioiTinh) {
		this.gioiTinh = gioiTinh;
	}

}
